package controller;
import modle.bo.AdminSearchBO;
import modle.vo.AdminInfoVO;
import modle.vo.AdminLoginVO;
import com.alibaba.druid.util.StringUtils;
import com.google.gson.Gson;
import modle.bo.AdminLoginBO;
import service.AdminService;
import service.AdminServiceImpl;
import util.HttpUtils;
import util.Result;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @Author: ZeYu
 * @Date: 2022/3/12 15:44
 */
@WebServlet("/api/admin/admin/*")
public class AdminServlet extends HttpServlet {

    private AdminService adminService=new AdminServiceImpl();
    Gson gson = new Gson();


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("post");
        String requestURI = request.getRequestURI();
        String op = requestURI.replace(request.getContextPath() + "/api/admin/admin/", "");
        if ("login".equals(op)){
            login(request,response);
        }else if("getSearchAdmins".equals(op)){
            getSearchAdmins(request,response);
        }
    }
    //1 获取请求参数 抓包 2 处理业务逻辑 3 返回响应 抓包
    private void getSearchAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        AdminSearchBO adminSearchBO = gson.fromJson(requestBody, AdminSearchBO.class);
        List<AdminInfoVO>adminInfoVOS= adminService.getSearchAdmins(adminSearchBO);
        response.getWriter().println(gson.toJson(Result.ok(adminInfoVOS)));

    }

    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求参数：自行解析请求体数据
        //inputStream如果不关tomcat会帮忙关掉
        //outputStream是需要关闭的
//        ServletInputStream inputStream = request.getInputStream();
//        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//        int length=0;
//        byte[]bytes=new byte[1024];
//        while ((length=inputStream.read(bytes))!=-1){
//            outputStream.write(bytes,0,length);
//        }
//        String requestBody=outputStream.toString("utf-8");
//        outputStream.close();
        String requestBody = HttpUtils.getRequestBody(request);

        AdminLoginBO loginBO = gson.fromJson(requestBody, AdminLoginBO.class);
//        System.out.println(loginBO);

        //校验
        if (StringUtils.isEmpty(loginBO.getEmail())||StringUtils.isEmpty(loginBO.getPwd())){
            Result result = new Result(10000, "用户名或密码为空", null);
            response.getWriter().println(gson.toJson(result));
        }
        //在响应体中设置一个json字符串
        int code= adminService.login(loginBO);
        if (code==200){
            Result result = new Result(0, null, new AdminLoginVO(loginBO.getEmail(), loginBO.getEmail()));
            response.getWriter().println(gson.toJson(result));




        }else if (code==404){
            Result result = new Result(10000, "密码不正确", null);
            response.getWriter().println(gson.toJson(result));

        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replace(request.getContextPath() + "/api/admin/admin/", "");
        if ("allAdmins".equals(op)){
            allAdmins(request,response);
        }
    }
    //任何一个接口的处理都可以分为三步走：一.获取请求参数（如果是key=value&key=value，那么可以直接使用API来获取）
    //2.如果有文件上传，那么也不可以使用API，commons-fileupload
    //3.如果提交的是json字符串，那么也是不可以使用的，自己解析请求体，gson转换
    //二.处理具体的业务逻辑
    //三.根据2.100上面的返回值要求，返回相应的响应体
    private void allAdmins(HttpServletRequest request, HttpServletResponse response)  {
        List<AdminInfoVO> adminInfoVOS= adminService.allAdmins();
        Result result = new Result(0, null, adminInfoVOS);
        String s = gson.toJson(result);
        System.out.println(s);
        try {
            response.getWriter().println(gson.toJson(result));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
